
package me.zhengjie.modules.vip.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.vip.domain.Members;
import me.zhengjie.modules.vip.service.MembersService;
import me.zhengjie.modules.vip.domain.vo.MembersQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author ck
* @date 2024-12-20
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "会员管理管理")
@RequestMapping("/api/members")
public class MembersController {

    private final MembersService membersService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('members:list')")
    public void exportMembers(HttpServletResponse response, MembersQueryCriteria criteria) throws IOException {
        membersService.download(membersService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询会员管理")
    @ApiOperation("查询会员管理")
    @PreAuthorize("@el.check('members:list')")
    public ResponseEntity<PageResult<Members>> queryMembers(MembersQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(membersService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增会员管理")
    @ApiOperation("新增会员管理")
    @PreAuthorize("@el.check('members:add')")
    public ResponseEntity<Object> createMembers(@Validated @RequestBody Members resources){
        membersService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改会员管理")
    @ApiOperation("修改会员管理")
    @PreAuthorize("@el.check('members:edit')")
    public ResponseEntity<Object> updateMembers(@Validated @RequestBody Members resources){
        membersService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除会员管理")
    @ApiOperation("删除会员管理")
    @PreAuthorize("@el.check('members:del')")
    public ResponseEntity<Object> deleteMembers(@RequestBody List<Integer> ids) {
        membersService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}